﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Animation;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using StoveTop.Models;

/* Display Recipe Information */

namespace StoveTop.Screens
{
	[Activity (Label = "StoveTopRecipeScreen")]			
	public class StepScreen : Activity
	{
		private static int STEPLAYOUTID = 24;
		private static int TIMERLAYOUTID = 25;

		private static string CURRENTSTEP = "step";

		private List<int> stepIds;
		private int currentStep;

		StoveTopDBHelper database;

		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate (bundle);

			LinearLayout layout = new LinearLayout (this);

			Display orient = this.WindowManager.DefaultDisplay;

			if (orient.Width < orient.Height)
			{
				layout.Orientation = Orientation.Vertical;
			}
			else
			{
				layout.Orientation = Orientation.Horizontal;
			}

			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (-1, -1, 1);

			FrameLayout stepFragmentLayout = new FrameLayout (this);
			stepFragmentLayout.Id = STEPLAYOUTID;
			stepFragmentLayout.LayoutParameters = layoutParams;

			FrameLayout timerFragmentLayout = new FrameLayout (this);
			timerFragmentLayout.Id = TIMERLAYOUTID;
			timerFragmentLayout.LayoutParameters = layoutParams;

			layout.AddView (stepFragmentLayout);
			layout.AddView (timerFragmentLayout);

			this.SetContentView (layout);

			database = new StoveTopDBHelper (this);

			this.stepIds = new List<int> (Intent.GetIntArrayExtra ("stepIds"));
			this.currentStep = -1;

			if (bundle == null)
			{
				this.NextStep ();
			}
			else
			{
				this.currentStep = bundle.GetInt (CURRENTSTEP);
				StepFragment fragment = this.FragmentManager.FindFragmentByTag ("step") as StepFragment;
				fragment.NextEvent += (object sender, EventArgs e) => this.NextStep();
				fragment.PrevEvent += (object sender, EventArgs e) => this.PrevStep();
			}
		}

		protected override void OnSaveInstanceState (Bundle outState)
		{
			base.OnSaveInstanceState (outState);
			outState.PutInt (CURRENTSTEP, this.currentStep);
		}
				
		private void NextStep()
		{
			if (this.currentStep < this.stepIds.Count - 1)
			{
				this.currentStep++;

				Step step = this.database.GetStep (this.stepIds [this.currentStep]);

				StepFragment stepFrag = StepFragment.CreateStep (step.GetStepNum(), step.GetInstructions());

				FragmentTransaction transaction = this.FragmentManager.BeginTransaction ();
				transaction.SetCustomAnimations (Resource.Animation.enter_from_right, Resource.Animation.exit_to_left);
				transaction.Replace (STEPLAYOUTID, stepFrag, "step").Commit ();

				TimerFragment timerFrag = TimerFragment.CreateTimer (step.GetTimer ());
				this.FragmentManager.BeginTransaction ().Replace (TIMERLAYOUTID, timerFrag, "timer").Commit ();

				stepFrag.NextEvent += (object sender, EventArgs e) => this.NextStep();
				stepFrag.PrevEvent += (object sender, EventArgs e) => this.PrevStep();
			}
		}

		private void PrevStep()
		{
			if (this.currentStep > 0)
			{
				this.currentStep--;

				Step step = this.database.GetStep (this.stepIds [this.currentStep]);

				StepFragment stepFrag = StepFragment.CreateStep (step.GetStepNum(), step.GetInstructions());

				FragmentTransaction transaction = this.FragmentManager.BeginTransaction ();
				transaction.SetCustomAnimations (Resource.Animation.enter_from_left, Resource.Animation.exit_to_right);
				transaction.Replace (STEPLAYOUTID, stepFrag, "step").Commit ();

				TimerFragment timerFrag = TimerFragment.CreateTimer (step.GetTimer ());
				this.FragmentManager.BeginTransaction ().Replace (TIMERLAYOUTID, timerFrag, "timer").Commit ();

				stepFrag.NextEvent += (object sender, EventArgs e) => this.NextStep();
				stepFrag.PrevEvent += (object sender, EventArgs e) => this.PrevStep();
			}
		}
	}
}

